<?php
/**
* Database table for abstracting schemas and in future versions will perform database migrations
* 
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @license New BSD License
* @version 0.1
* @category Shuffler  
* @package Shuffler_Db 
*/
class Shuffler_Db_Table extends Shuffler_Db_Abstract
{
    protected $name;
    protected $primary;
    protected $fields = array();
    
    public function __construct( Shuffler_Db_Factory $factory, $name )
    {
        parent::__construct( $factory );
        $this->name = $name;
    }
    
    /**
    * Create a field and add it
    * 
    * @param string name of field
    * @param string type of field ( varchar, int, etc.. )
    * @param integer optional length
    * @param array an array of options to set
    */
    public function createField( $name, $type = 'varchar', $length = 150, array $options = array() )
    {
        $field = new Shuffler_Db_Field( $this, $name );
        $field->setType( $type );
        $field->setLength( $length );
        foreach( $options as $key => $value )
        {
            $command = 'set' . ucfirst( $key );
            if( !method_exists( $field, $command ) )
            {
                throw new Exception( 'Method does not exist ' . get_class( $field ) . '->' . $command );
            }
            $field->$command( $value );
        }
        $this->fields[ $name ] = $field;
        return $field;
    }
    
    /**
    * Set a field as primary key
    * 
    * @param Shuffler_Db_Field field
    */
    public function setPrimary( Shuffler_Db_Field $field )
    {
        $this->primary = $field;
        return $this;
    }
    
    /**
    * Insert an array of values into the table
    */
    public function insert( array $values )
    {
        $fields = $this->getFields();
        $sql = 'INSERT INTO `' . $this->name . '` ';
        $sql .= ' ( ' . $this->implodeIdentifiers( $fields ) . ' ) ';
        $sql .= ' VALUES ( ' . $this->implodeValues( $values ) . ' ); ';
        $this->factory->execute( $sql );
        return $this;
    }  
    
    /**
    * Get array of field names for this table
    * 
    * @return array of field name strings
    */
    public function getFields()
    {    
        return array_keys( $this->fields );
    }
    
    /**
    * Generate table CREATE SQL and execute
    */
    public function create()
    {
        $sql = sprintf(
            'CREATE TABLE %s ( ',
            $this->quoteIdentifier( $this->name )
        );
        $count = count( $this->fields );
        $i = 0;
        $hasIndex = (bool)( $this->primary instanceof Shuffler_Db_Field );
        foreach( $this->fields as $field )
        {
            $i++;
            $sql .= $this->quoteIdentifier( $field->getName() );
            if( $field->hasLength() )
            {
                $sql .= sprintf( ' %s (%d) ',
                    $field->getType(),
                    $field->getLength()
                );
            }
            else
            {
                $sql .= sprintf( ' %s  ',
                    $field->getType()
                );
            }
            $sql .= $field->getNull() ? 'NULL' : 'NOT NULL';
            if( $field->hasDefault() )
            {
                $sql .= ' default ' . $this->quote( $field->getDefault() );
            }
            $sql .= ' ' . $field->getExtra();
            if( $hasIndex || $i < $count )
            {
                $sql .= ',';
            }
        }
        if( $hasIndex )
        {
            $sql .= 'PRIMARY KEY  ( ' . $this->quoteIdentifier( $this->primary->getName() ) . ' )' ;
        }
        $sql .= ') ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
        $this->factory->execute( $sql );
    }

}